# 会员信息管理的视图文件
from django.shortcuts import render
from django.http import *
from myadmin.models import Member
from django.core.paginator import Paginator
from django.db.models import Q
from datetime import datetime


def index(request,pindex=1):
    """浏览信息"""
    umod = Member.objects
    ulist = umod.filter(status__lt=9)  # 只要是9状态就不显示
    # 维持搜索条件
    mywhere = []
    # 获取判断并封装状态status搜索条件
    status = request.GET.get('status', '')
    if status != '':
        ulist = ulist.filter(status=status)
        mywhere.append("status="+status)

    ulist = ulist.order_by("id")
    # 执行分页处理
    pindex = int(pindex)
    page = Paginator(ulist, 5)  # 以5条数据为一页
    maxpages = page.num_pages  # 获取最大页数
    # 判断当前页是否越界
    if pindex > maxpages:
        pindex = maxpages
    if pindex < 1:
        pindex = 1
    # 获取当前页数据
    list2 = page.page(pindex)
    # 获取页码列表信息
    plist = page.page_range
    context = {"memberlist":list2,"plist":plist,"pindex":pindex,"maxpages":maxpages,"mywhere":mywhere}
    return render(request,"myadmin/member/index.html",context)

def delete(request,uid=0):
    """执行信息删除"""
    try:
        ob = Member.objects.get(id=uid)
        ob.status = 9
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info':"删除成功"}
    except Exception as err:
        print(err)
        context = {'info':"删除失败"}
    return render(request,"myadmin/info.html",context)

